<template>
  <div>
    <el-card style="width: 50%">
      <el-form :model="user" label-width="100px" style="padding-right: 50px">
        <div style="margin: 15px; text-align: center">
          <el-upload
                  class="avatar-uploader"
                  :action="$baseUrl + '/files/upload'"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" placeholder="用户名" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="user.name" placeholder="姓名" ></el-input>
        </el-form-item>
        <el-form-item label="学院" prop="collegeId">
          <el-select v-model="form.collegeId" placeholder="请选择学院"  @change="loadSpeciality()" style="width: 100%">
            <el-option v-for="item in collegeData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业" prop="specialityId">
          <el-select v-model="form.specialityId" placeholder="请选择专业"  @change="loadClasses()"style="width: 100%">
            <el-option v-for="item in specialityData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="classesId">
          <el-select v-model="form.classId" placeholder="请选择班级" style="width: 100%" >
            <el-option v-for="item in classesData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <el-button type="primary" @click="save">保 存</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: "StudentPerson",
    data() {
      return {
        form: {},
        user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
        ids: [],
        username: null,
        collegeData: [],
        specialityData: [],
        classesData: []
      }
    },
    created() {
      this.loadCollege()
    },
    methods: {
      loadCollege() {
        this.$request.get('/college/selectAll').then(res => {
          if (res.code ==='200') {
            this.collegeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      loadSpeciality() {
        this.$request.get('/speciality/selectById/'+this.form.collegeId).then(res => {
          if (res.code === '200') {
            this.specialityData = res.data
          } else {
            this.$message.error(res.data)
          }
        })
      },
      loadClasses() {
        this.$request.get('/classes/selectById/'+this.form.specialityId).then(res => {
          if (res.code === '200') {
            this.classesData = res.data
          } else {
            this.$message.error(res.data)
          }
        })
      },
      // save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      //   this.$refs.formRef.validate((valid) => {
      //     if (valid) {
      //       this.$request({
      //         url: this.form.id ? '/student/update' : '/student/add',
      //         method: this.form.id ? 'PUT' : 'POST',
      //         data: this.form
      //       }).then(res => {
      //         if (res.code === '200') {  // 表示成功保存
      //           this.$message.success('保存成功')
      //         } else {
      //           this.$message.error(res.msg)  // 弹出错误的信息
      //         }
      //       })
      //     }
      //   })
      // },
      save() {
        // 保存当前的用户信息到数据库
        this.$request.put('/student/update', this.user).then(res => {
          if (res.code === '200') {
            // 成功更新
            this.$message.success('保存成功')
            // 更新浏览器缓存里的用户信息
            localStorage.setItem('xm-user', JSON.stringify(this.user))
            // 触发父级的数据更新
            this.$emit('update:user')
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      handleAvatarSuccess(response, file, fileList) {
        // 把user的头像属性换成上传的图片的链接
        this.$set(this.user, 'avatar', response.data)
      },
    }
  }
</script>

<style scoped>
  /deep/.el-form-item__label {
    font-weight: bold;
  }
  /deep/.el-upload {
    border-radius: 50%;
  }
  /deep/.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border-radius: 50%;
  }
  /deep/.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
    border-radius: 50%;
  }
  .avatar {
    width: 120px;
    height: 120px;
    display: block;
    border-radius: 50%;
  }
</style>